

package com.xkk.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.xkk.dao.ITmessageDao;
import com.xkk.dbconn.DBConn;
import com.xkk.po.Tmessage;

public class TmessageDaoimpl implements ITmessageDao {
	
	private DBConn dbconn;

	public TmessageDaoimpl() {
		super();
		dbconn = new DBConn();
	}

	@SuppressWarnings("unused")
	@Override
	public void add(Tmessage obj) {
		// TODO Auto-generated method stub
		
		int messagefrom = obj.getMessagefrom();
		int messageto = obj.getMessageto();
		String messagebody = obj.getMessagebody();
		Date messagedate = obj.getMessagedate();
		
		String strSQL = "insert into tmessage values(null,?,?,?,?)";
		this.dbconn.execOther(strSQL, new Object[]{messagefrom,messageto,messagebody,messagedate});
	}

	@Override
	public void deleteByid(int logid) {
		// TODO Auto-generated method stub
		
		String strSQL = "delete from tmessage where logid=?";
		this.dbconn.execOther(strSQL, new Object[]{logid});
		
	}

	@Override
	public void update(Tmessage obj) {
		// TODO Auto-generated method stub
		
		int logid = obj.getLogid();
		
		int messagefrom = obj.getMessagefrom();
		int messageto = obj.getMessageto();
		String messagebody = obj.getMessagebody();
		Date messagedate = obj.getMessagedate();
		
		String strSQL = "update tmessage set messagefrom=? ,messageto = ?,messagebody = ?,messagedate = ?  where logid=?";
		this.dbconn.execOther(strSQL, new Object[]{messagefrom,messageto,messagebody,messagedate,logid});
		
		
	}

	@Override
	public List<Tmessage> findAll() {
		//
		
		List<Tmessage> lstClasses = new ArrayList<Tmessage>();
		String strSQL = "select * from tmessage  ";
		ResultSet rs = this.dbconn.execQuery(strSQL, new Object[]{});
		try {
			while(rs.next()){
				Tmessage obj = new Tmessage();
				obj.setLogid(rs.getInt(1));
				obj.setMessagefrom(rs.getInt(2));
				obj.setMessageto(rs.getInt(3));
				obj.setMessagebody(rs.getString(4));
				obj.setMessagedate(rs.getDate(5));
				lstClasses.add(obj);
			}
			return lstClasses;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			this.dbconn.closeConn();
		}
	}

	@Override
	public List<Tmessage> findByid(int logid) {
	
		List<Tmessage> lstClasses = new ArrayList<Tmessage>();
		String strSQL = "select * from tmessage where logid = ?";
		ResultSet rs = this.dbconn.execQuery(strSQL, new Object[]{logid});
		try {
			while(rs.next()){
				Tmessage obj = new Tmessage();
				obj.setLogid(rs.getInt(1));
				obj.setMessagefrom(rs.getInt(2));
				obj.setMessageto(rs.getInt(3));
				obj.setMessagebody(rs.getString(4));
				obj.setMessagedate(rs.getDate(5));
				lstClasses.add(obj);
			}
			return lstClasses;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			this.dbconn.closeConn();
		}
		
	}

	
	
}
